package ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.subpanels.detailPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import ch.zbw.pra.sysventory.domain.model.Computer;
import ch.zbw.pra.sysventory.domain.model.Nic;
import ch.zbw.pra.sysventory.domain.model.Printer;
import ch.zbw.pra.sysventory.domain.model.Software;

public class DetailTablePanel extends JPanel {
	public enum DetailTableEnum {
		PRINTER, SOFTWARE, NIC
	};

	private static final long serialVersionUID = 7852358863481231609L;
	private Computer computer;
	private List<String> header;
	private JTable detailTable;
	private JScrollPane scrolly;
	private DetailTableModel detailTableModel;

	public DetailTablePanel(Computer computer, DetailTableEnum theEnum) {
		this.computer = computer;
		this.detailTable = new JTable();
		this.detailTable.setBorder(BorderFactory.createLineBorder(Color.black));
		this.scrolly = new JScrollPane(this.detailTable);
		this.header = new ArrayList<String>();
		this.detailTableModel = new DetailTableModel();
		init();
		createOneOfBothTable(theEnum);
	}

	public void init() {
		this.scrolly.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		this.scrolly.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		this.scrolly.setPreferredSize(new Dimension(450, 310));
		this.add(this.scrolly, BorderLayout.CENTER);
		// this.detailTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		// this.detailTable.setRowHeight(25);
		// this.detailTable.setro
	}

	public void createOneOfBothTable(DetailTableEnum theEnum) {

		if (theEnum == DetailTableEnum.PRINTER) {
			createPrinterTable();
		} else if (theEnum == DetailTableEnum.SOFTWARE) {
			createSoftwareTable();
		} else {
			createNicTable();
		}

	}

	public void createPrinterTable() {
		if (this.computer != null) {
			this.header.add("Drucker-Name");
			this.header.add("Treiber-Name");
			this.header.add("Treiber-Version");
			List<Printer> printers = this.computer.getPrinters();

			List<List<String>> row = new ArrayList<List<String>>();
			for (int i = 0; i < printers.size(); i++) {
				List<String> aPrinter = new ArrayList<String>();
				aPrinter.add(printers.get(i).getNamePrinter());
				aPrinter.add(printers.get(i).getDriverNamePrinter());
				aPrinter.add(printers.get(i).getDriverVersionPrinter());
				row.add(aPrinter);
			}

			this.detailTableModel = new DetailTableModel(row, this.header);
			this.detailTable.setModel(this.detailTableModel);
			this.detailTable.setDefaultRenderer(new ArrayList<String>().getClass(), this.detailTableModel);
		}
	}

	public void createSoftwareTable() {
		if (this.computer != null) {
			this.header.add("Software-Name");
			this.header.add("Hersteller-Name");
			this.header.add("Software-Version");
			List<Software> softwares = this.computer.getSoftwares();

			List<List<String>> row = new ArrayList<List<String>>();
			for (int i = 0; i < softwares.size(); i++) {
				List<String> aSoftware = new ArrayList<String>();
				aSoftware.add(softwares.get(i).getNameSoftware());
				aSoftware.add(softwares.get(i).getVendor().getNameVendorSoftware());
				aSoftware.add(softwares.get(i).getVersionSoftware());
				row.add(aSoftware);
			}
			this.detailTableModel = new DetailTableModel(row, this.header);
			this.detailTable.setModel(this.detailTableModel);
			this.detailTable.setDefaultRenderer(new ArrayList<String>().getClass(), this.detailTableModel);
		}
	}

	public void createNicTable() {
		if (this.computer != null) {
			this.header.add("NIC-Bezeichnung");
			this.header.add("IPv4");
			this.header.add("IPv6");
			this.header.add("Subnetz");
			this.header.add("MAC-Adresse");
			this.header.add("DHCP");

			List<Nic> nics = this.computer.getNics();

			List<List<String>> row = new ArrayList<List<String>>();
			for (int i = 0; i < nics.size(); i++) {
				List<String> aNic = new ArrayList<String>();
				aNic.add(nics.get(i).getNameNic());
				aNic.add(nics.get(i).getIpv4Nic());
				aNic.add(nics.get(i).getIpv6Nic());
				aNic.add(nics.get(i).getSubnetNic());
				aNic.add(nics.get(i).getMacAdressNic());
				aNic.add(nics.get(i).getStandardGatewayNic());
				row.add(aNic);
			}
			this.detailTableModel = new DetailTableModel(row, this.header);
			this.detailTable.setModel(this.detailTableModel);
			this.detailTable.setDefaultRenderer(new ArrayList<String>().getClass(), this.detailTableModel);
		}

	}
}
